package com.nanyang.tms.service.forecast.impl;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.forecast.ForecastFrontDetailRequest;
import com.nanyang.tms.api.dto.request.forecast.ForecastFrontPageQueryRequest;
import com.nanyang.tms.api.dto.request.forecast.ForecastPageQueryRequest;
import com.nanyang.tms.api.dto.request.forecast.ForecastUpdateStateRequest;
import com.nanyang.tms.api.dto.request.product.ProductUpdateStateRequest;
import com.nanyang.tms.api.dto.response.forecast.*;
import com.nanyang.tms.api.service.forecast.ForecastQueryRpcService;
import com.nanyang.tms.domain.forecast.ForecastBaseVO;
import com.nanyang.tms.domain.forecast.ForecastDO;
import com.nanyang.tms.domain.forecast.ForecastQuery;
import com.nanyang.tms.domain.forecast.ForecastVO;
import com.nanyang.tms.domain.product.ProductInfoDO;
import com.nanyang.tms.manager.forecast.ForecastManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service(version = "1.0.0", timeout = 60000, retries = 0, validation = "true")
public class ForecastQueryRpcServiceImpl implements ForecastQueryRpcService {

   @Autowired
   private ForecastManager forecastManager;

	@Override
	public ApiResult<PageBean<ForecastPageResponse>> listForecast(ForecastPageQueryRequest param) {
		ApiResult<PageBean<ForecastPageResponse>> apiResult = new ApiResult<>();
		try {
			param.requestCheck();
			ForecastQuery query = new ForecastQuery();
			BeanUtils.copyProperties(param, query);
			PageBean<ForecastVO> forecastList = forecastManager.listForecast(query);
			PageBean<ForecastPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
			resultList.setTotalItem(forecastList.getTotalItem());
			BeanUtils.copyListProperties(forecastList.getData(), resultList.getData(), ForecastPageResponse.class);
			apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			apiResult.setData(resultList);
		} catch (Exception e) {
			log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
			ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		return apiResult;
	}

	@Override
	public ApiResult<ForecastBaseResponse> selectByCode(String code) {
		ApiResult<ForecastBaseResponse> result = new ApiResult<>();
		try {
			ForecastBaseResponse forecastBaseResponse = new ForecastBaseResponse();
			ForecastBaseVO forecastBaseVO = forecastManager.selectByCode(code);

			ForecastResponse forecastResponse = new ForecastResponse();
			BeanUtils.copyProperties(forecastBaseVO.getForecastVO(),forecastResponse);
			forecastBaseResponse.setForecastResponse(forecastResponse);

			ForecastReceiverResponse forecastReceiverResponse = new ForecastReceiverResponse();
			BeanUtils.copyProperties(forecastBaseVO.getForecastReceiverVO(),forecastReceiverResponse);
			forecastBaseResponse.setForecastReceiverResponse(forecastReceiverResponse);

			ForecastSenderResponse forecastSenderResponse = new ForecastSenderResponse();
			BeanUtils.copyProperties(forecastBaseVO.getForecastSenderVO(),forecastSenderResponse);
			forecastBaseResponse.setForecastSenderResponse(forecastSenderResponse);

			List<ForecastDeclareResponse> forecastDeclareResponses = new ArrayList<>();
			BeanUtils.copyListProperties(forecastBaseVO.getForecastDeclareVOList(),forecastDeclareResponses,ForecastDeclareResponse.class);
			forecastBaseResponse.setForecastDeclareResponseList(forecastDeclareResponses);

			List<ForecastVolumeResponse> forecastVolumeResponses =new ArrayList<>();
			BeanUtils.copyListProperties(forecastBaseVO.getForecastVolumeVOList(),forecastVolumeResponses,ForecastVolumeResponse.class);
			forecastBaseResponse.setForecastVolumeResponseList(forecastVolumeResponses);
			result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			result.setData(forecastBaseResponse);
		} catch (Exception e) {
			log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
			ExceptionProcessorUtils.wrapperHandlerException(result, e);
		}
		return result;
	}

	@Override
	public ApiResult<PageBean<ForecastFrontPageResponse>> listFrontForecast(ForecastFrontPageQueryRequest param) {
		ApiResult<PageBean<ForecastFrontPageResponse>> apiResult = new ApiResult<>();
		try {
			param.requestCheck();
			ForecastQuery query = new ForecastQuery();
			BeanUtils.copyProperties(param, query);
			PageBean<ForecastVO> forecastList = forecastManager.listForecast(query);
			PageBean<ForecastFrontPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
			resultList.setTotalItem(forecastList.getTotalItem());
			BeanUtils.copyListProperties(forecastList.getData(), resultList.getData(), ForecastFrontPageResponse.class);
			apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			apiResult.setData(resultList);
		} catch (Exception e) {
			log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
			ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		return apiResult;
	}

	@Override
	public ApiResult<ForecastBaseResponse> selectByCompanyCode(ForecastFrontDetailRequest param) {
		ApiResult<ForecastBaseResponse> result = new ApiResult<>();
		try {
			param.requestCheck();
			ForecastQuery query = new ForecastQuery();
			BeanUtils.copyProperties(param, query);
			ForecastBaseVO forecastBaseVO = forecastManager.selectByCompanyCode(query);
			ForecastBaseResponse forecastBaseResponse = new ForecastBaseResponse();
			ForecastResponse forecastResponse = new ForecastResponse();
			BeanUtils.copyProperties(forecastBaseVO.getForecastVO(),forecastResponse);
			forecastBaseResponse.setForecastResponse(forecastResponse);

			ForecastReceiverResponse forecastReceiverResponse = new ForecastReceiverResponse();
			BeanUtils.copyProperties(forecastBaseVO.getForecastReceiverVO(),forecastReceiverResponse);
			forecastBaseResponse.setForecastReceiverResponse(forecastReceiverResponse);

			ForecastSenderResponse forecastSenderResponse = new ForecastSenderResponse();
			BeanUtils.copyProperties(forecastBaseVO.getForecastSenderVO(),forecastSenderResponse);
			forecastBaseResponse.setForecastSenderResponse(forecastSenderResponse);

			List<ForecastDeclareResponse> forecastDeclareResponses = new ArrayList<>();
			BeanUtils.copyListProperties(forecastBaseVO.getForecastDeclareVOList(),forecastDeclareResponses,ForecastDeclareResponse.class);
			forecastBaseResponse.setForecastDeclareResponseList(forecastDeclareResponses);

			List<ForecastVolumeResponse> forecastVolumeResponses =new ArrayList<>();
			BeanUtils.copyListProperties(forecastBaseVO.getForecastVolumeVOList(),forecastVolumeResponses,ForecastVolumeResponse.class);
			forecastBaseResponse.setForecastVolumeResponseList(forecastVolumeResponses);
			result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			result.setData(forecastBaseResponse);
		} catch (Exception e) {
			log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
			ExceptionProcessorUtils.wrapperHandlerException(result, e);
		}
		return result;
	}
	@Override
	public ApiResult<Boolean> cancelState(ForecastUpdateStateRequest param) {
		ApiResult<Boolean> result = new ApiResult<>();
		try {
			param.requestCheck();
			ForecastDO forecastDO = new  ForecastDO();
			BeanUtils.copyProperties(param,forecastDO);
			forecastDO.setGmtModifiedPin(param.getPin());
			forecastManager.cancelState(forecastDO);
			result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			result.setData(true);
		} catch (Exception e) {
			log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
			ExceptionProcessorUtils.wrapperHandlerException(result, e);
		}
		return result;
	}

	@Override
	public ApiResult<String> printTag(ForecastFrontDetailRequest param) {
		ApiResult<String> result = new ApiResult<>();
		try {
			param.requestCheck();
			ForecastQuery query = new ForecastQuery();
			BeanUtils.copyProperties(param, query);
			result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			result.setData(forecastManager.printTag(query));
		} catch (Exception e) {
			log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
			ExceptionProcessorUtils.wrapperHandlerException(result, e);
		}
		return result;
	}

	@Override
	public ApiResult<String> printList(ForecastFrontDetailRequest param) {
		ApiResult<String> result = new ApiResult<>();
		try {
			param.requestCheck();
			ForecastQuery query = new ForecastQuery();
			BeanUtils.copyProperties(param, query);
			result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			result.setData(forecastManager.printList(query));
		} catch (Exception e) {
			log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
			ExceptionProcessorUtils.wrapperHandlerException(result, e);
		}
		return result;
	}
	@Override
	public ApiResult<Boolean> syncPrintTag(String forecastCode) {
		ApiResult<Boolean> result = new ApiResult<>();
		try {
			forecastManager.syncPrintTag(forecastCode);
			result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			result.setData(true);
		} catch (Exception e) {
			log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
			ExceptionProcessorUtils.wrapperHandlerException(result, e);
		}
		return result;
	}
}

